/**
 *@description 系统管理页面操作页面js
 *@author XieXing
 *@date 2020/8/23 15:33
 */
var table, form, layer, laydate, layedit, tree;//当前页面全局定义 TODO 继续优化
var module = {
    /**
     *@description 菜单操作接口url集合 统一管理
     *@author XieXing
     *@date 2020/8/23 15:35
     */
    URL: {
        getDataListUrl: '/optLog/getOptLogList',//获取菜单信息
        getOptLogDetail: "/optLog/getOptLogDetail",//保存菜单信息
    },
    /**
     *@description 初始化
     *@author XieXing
     *@date 2020/8/23 15:36
     */
    init: function () {
        layui.use(['form', 'table', 'layer', 'laydate', 'layedit', 'tree'], function () {
            tree = layui.tree;
            table = layui.table;
            form = layui.form;
            layer = layui.layer;
            laydate = layui.laydate;
            layedit = layui.layedit;
            var pageData = page.pageData();
            table.render({
                elem: '#datagrid',
                id: 'datagrid',
                url: module.URL.getDataListUrl,/*换成你的接口*/
                method: "GET",
                cols: [[
                    {type: 'radio', fixed: 'left'},
                    {type: 'numbers', title: '序号', fixed: "left"},
                    {
                        field: 'optUserName',
                        title: '操作人',
                        align: 'center',
                        width: '10%'
                    },
                    {field: 'optTime', title: '操作时间', align: 'center', width: '15%'},
                    {
                        field: 'optUrl',
                        title: '请求路径',
                        align: 'center',
                        width: '20%'
                    },
                    {field: 'optIp', title: '请求ip', align: 'center', width: '10%'},
                    {
                        field: 'optHttpMethod',
                        title: '请求方式',
                        align: 'center',
                        width: '10%',
                        templet: function (d) {
                            if (d.optHttpMethod === 1) {
                                return "GET";
                            } else if (d.optHttpMethod === 2) {
                                return "POST";
                            } else if (d.optHttpMethod === 3) {
                                return "PUT";
                            } else {
                                return "DELETE";
                            }
                        }
                    },
                    {field: 'optClassMethod', title: '请求对应的class方法', align: 'center', width: '30%'},
                    // {field: 'optReamrk', title: '备注', align: 'center', width: '13%'},
                    {
                        field: 'detail',
                        title: '详情',
                        align: 'center',
                        width: '10%',
                        fixed:"right",
                        templet: function () {
                            return "<button type='button' class='layui-btn layui-btn-normal' lay-event='detail'>详情</button>"
                        }
                    }
                ]],
                height: pageData.tableHeight,
                limit: pageData.limit,
                limits: pageData.limits,
                page: true,
                response: {
                    statusCode: 200
                },
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.stateInfo, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                },
                done: function (res, curr, count) {
                    var perPageMax = $(".layui-laypage-limits select option:last-child").val();//每页最大条数
                    page.tableLoadAll(count, perPageMax);//加载全部
                }
            });

            //监听单元格事件
            table.on('tool(datagrid)', function (obj) {
                if (obj.event === 'detail') {
                    module.edit_model("操作日志详情查看",module.URL.getOptLogDetail,obj.data);
                }
            });

            /**
             *@description 点击查询
             *@author XieXing
             *@date 2020/7/30 11:34
             */
            $("#btnSearch").on("click", function () {
                var qm = {};
                $("#queryForm").serializeArray().forEach(function (each) {
                    qm[each.name] = each.value;
                });
                var cur = parseInt($(".layui-laypage-skip input").val()) || 1;//当前页
                var limit = parseInt($(".layui-laypage-limits select").val()) || 10;//当前每页条数
                table.reload('datagrid', {
                    where: qm,
                    page: {curr: cur, limit: limit}
                });
                return false;
            });
        });
    },
    /**
     *@description 编辑弹出层
     *@author XieXing
     *@date 2020/8/18 22:00
     */
    edit_model: function (modelName, url, rowData) {
        layer.open({
            type: 1,
            area: ['600px', '500px'],
            title: modelName,
            content: $("#dataModel"),
            offset: ['20px'],
            btn: ['提交', '取消'],
            btn1: function (index, layero) {
                $("#dataModel form")[0].reset();
                layer.closeAll();
                return false;
            },
            btn2: function (index, layero) {
                $("#dataModel form")[0].reset();
                layer.closeAll();
                return false;
            },
            cancel: function (layero, index) {
                $("#dataModel form")[0].reset();
                layer.closeAll();
            },
            success: function (layero, index) {
                layero.find("textarea").css("height","100px");
                layero.find("textarea").css("width","450px");
                $.post(url, {'optId':rowData.id}, function (result) {
                    if(result.code === 200){
                        layero.find("textarea[name='optReqArgs']").val(result.data['optReqArgs']);
                        layero.find("textarea[name='optResData']").val(result.data['optResData']);
                        layero.find("textarea[name='optReamrk']").val(rowData['optReamrk']);
                    }
                });
            }
        });
    },
    /**
     *@description 对象回填数据
     *@author XieXing
     *@date 2020/8/17 15:02
     */
    getData: function (data) {
        for (var k in data) {
            $("input[name=" + k + "],select[name=" + k + "],textarea[name=" + k + "]").val(data[k]);
        }
        form.render();
    }

};
module.init();//运行初始化函数
